package Food_dao;



import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Food_model.User;
import Food_util.DBHelp;



public class UserDao {
		public void register(User u) {
			Connection con = DBHelp.getConnection();
			try {
				PreparedStatement  ps = con.prepareStatement("insert into user(username,password,email,gender)values(?,?,?,?)");
				ps.setString(1, u.getUsername());
				ps.setString(2, u.getPassword());
				ps.setString(3, u.getEmail());
				ps.setString(4, u.getGender());
				ps.executeUpdate();
				DBHelp.close(null, ps, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		public boolean validate(String username,String password) {
			boolean flag = false;
			Connection con = DBHelp.getConnection();
			try {
				PreparedStatement ps = con.prepareStatement("select * from user where username=? and password=?");
				ps.setString(1, username);
				ps.setString(2, password);
				ResultSet rs = ps.executeQuery();
				if(rs.next()) {
					flag = true;
				}else {
					flag = false;
				}
				DBHelp.close(rs, ps, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return flag;
			
		}
}
